//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace WorkflowApplicationModels
{
    using Microsoft.Research.DataLayer;
    using System;
    using System.Globalization;

    /// <summary>
    /// The workflow model.
    /// </summary>
    public class WorkflowModel : BaseModel
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WorkflowModel"/> class.
        /// </summary>
        /// <param name="activity">The activity.</param>
        public WorkflowModel(Activity activity)
        {
            this.Name = activity.Name;
            this.Label = (string.IsNullOrEmpty(activity.VersionLabel)) ? this.Name : string.Format(CultureInfo.CurrentCulture, "{0} ({1})", activity.Name, activity.VersionLabel);
            this.Id = (activity as IObject).ID;
            this.ItemIcon = BaseModel.CreateImage(@"pack://Application:,,,/Microsoft.Research.ScientificWorkflow.TridentUtilities;component/Images/WorkflowApplication/workflowIcon.png");
            this.VersionLabel = activity.VersionLabel;
        }

        /// <summary>
        /// The version label.
        /// </summary>
        public string VersionLabel { get; private set; }

        /// <summary>
        /// The modified time of the workflow.
        /// </summary>
        public DateTime ModifiedTime { get; private set; }

        /// <summary>
        /// Sorts the activity list based on workflow name and the modified time.
        /// Implementation of Icomparer object
        /// </summary>
        /// <param name="activityA">Activity</param>
        /// <param name="activityB">Activity</param>
        /// <returns></returns>
        public static int WorkflowComparer(WorkflowModel first, WorkflowModel second)
        {
            int result = 0;
            string firstStringToCompare = first.Name ;
            string secondStringToCompare = second.Name;
            int compareValue = string.Compare(firstStringToCompare, secondStringToCompare, StringComparison.OrdinalIgnoreCase);
            if (compareValue != 0)
            {
                result = compareValue > 0 ? 1 : -1;
            }
            else
            {
                compareValue = DateTime.Compare(first.ModifiedTime, second.ModifiedTime);
                result = compareValue > 0 ? -1 : 1;
            }
            return result;
        }
    }
}
